package com.youlian.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;

public class StringHelper {

	private static final String[] SPECIAL_ENTITIES = new String[] { "&quot;",
			"&amp;", "&lt;", "&gt;", "&nbsp;", "&(#|\\w){2,8};" };
	private static final String[] ENTITY_STRINGS = new String[] { "\"", "&",
			"<", ">", " ", "" };

	public static final String LINESYMBOL = "\n";

	public static final String EMPTYSYMBOL = "";

	public static final String DOCUMENTSYMBOL = "/";

	public static String formatTime(int t) {
		if (t < 10)
			return "0" + t;
		return "" + t;
	}

	public static Spannable strikeThrough(String string) {
		Spannable word = new SpannableString(string);
		word.setSpan(new StrikethroughSpan(), 0, word.length(), 0);
		return word;
	}

	public static Spannable backgroundColor(String string, int color) {
		Spannable word = new SpannableString(string);
		word.setSpan(new BackgroundColorSpan(color), 0, word.length(), 0);
		return word;
	}

	public static Spannable foregroundColor(String string, int start, int end,
			int color) {
		Spannable word = new SpannableString(string);
		word.setSpan(new ForegroundColorSpan(color), start, end, 0);
		return word;
	}

	public static String inputStreamToString(InputStream is) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = reader.readLine()) != null) {

			sb.append(line);
		}
		is.close();
		return sb.toString();
	}

	public static String[] parseInfo(String line, String split, int length) {
		if (TextUtils.isEmpty(line))
			return null;
		String[] data = line.trim().split(split, length);
		return data.length == length ? data : null;
	}

	public static String htmlToText(String sourceStr) {
		if (Utils.notNull(sourceStr)) {
			sourceStr = sourceStr.replaceAll("&#039;", "'")
					.replaceAll("&amp;", "&").replaceAll("&nbsp;", " ")
					.replaceAll("</(?i)p>", LINESYMBOL)
					.replaceAll("<(?i)br\\s?/?>", LINESYMBOL)
					.replaceAll("</(?i)h\\d>", LINESYMBOL)
					.replaceAll("</(?i)tr>", LINESYMBOL)
					.replaceAll("<!--.*?-->", EMPTYSYMBOL)
					.replaceAll("<[^>]+>", EMPTYSYMBOL);
		}
		return sourceStr;
	}

	/**
	 * 将带有HTML标记的数据转换为纯文本格式
	 *
	 * @param sourceStr
	 * @return
	 */
	public static String getTextForHtml(String sourceStr) {
		String lineSymbol = "\n";
		String emptySymbol = "";
		Object obj = null;
		if (!TextUtils.isEmpty(sourceStr)) {
			sourceStr = sourceStr
					.replaceAll("<(?i)head[^>]*?>[\\s\\S]*?</(?i)head>",
							emptySymbol)
					.replaceAll("<(?i)style[^>]*?>[\\s\\S]*?</(?i)style>",
							emptySymbol)
					.replaceAll("<(?i)script[^>]*?>[\\s\\S]*?</(?i)script>",
							emptySymbol).replaceAll("</(?i)div>", lineSymbol)
					.replaceAll("</(?i)p>", lineSymbol)
					.replaceAll("<(?i)br\\s?/?>", lineSymbol)
					.replaceAll("</(?i)h\\d>", lineSymbol)
					.replaceAll("</(?i)tr>", lineSymbol)
					.replaceAll("<!--.*?-->", emptySymbol)
					.replaceAll("<[^>]+>", emptySymbol);
			obj = "\r\n\\s*";
			sourceStr = sourceStr.replaceAll(((String) (obj)), lineSymbol);
			int i = 0;
			do {
				int temp = SPECIAL_ENTITIES.length;
				if (i >= temp)
					break;
				obj = SPECIAL_ENTITIES[i];
				String s3 = ENTITY_STRINGS[i];
				sourceStr = sourceStr.replaceAll(((String) (obj)), s3);
				i++;
			} while (true);
			obj = sourceStr;
		}
		return obj.toString();
	}

	/**
	 * 去除HTML标记
	 *
	 * @param sourceStr
	 * @return
	 */
	public static String removeHtmlTag(String sourceStr) {
		if (Utils.notNull(sourceStr)) {
			sourceStr = sourceStr.replaceAll("<[^>]+>", "");
		}
		return sourceStr;
	}

	/**
	 * 得到格式化时间
	 *
	 * @param timeInSeconds
	 * @return
	 */
	public static String getFormatTimeMsg(int timeInSeconds) {
		int hours, minutes, seconds;
		hours = timeInSeconds / 3600;
		timeInSeconds = timeInSeconds - (hours * 3600);
		minutes = timeInSeconds / 60;
		timeInSeconds = timeInSeconds - (minutes * 60);
		seconds = timeInSeconds;

		String minStr = String.valueOf(minutes);
		String secStr = String.valueOf(seconds);

		if (minStr.length() == 1)
			minStr = "0" + minStr;
		if (secStr.length() == 1)
			secStr = "0" + secStr;

		return (minStr + "分" + secStr + "秒");
	}

	/**
	 * 判断输入是否是数字
	 *
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		String strPattern = "[0-9]*";
		Pattern p = Pattern.compile(strPattern);
		Matcher m = p.matcher(str.trim());
		if (m.matches()) {
			return true;
		}
		return false;
	}

	/**
	 * 判断输入是否是数字或者字母
	 *
	 * @param str
	 * @return
	 */
	public static boolean isNumOrLetter(String str) {
		String strPattern = "^[A-Za-z0-9]+$";
		Pattern p = Pattern.compile(strPattern);
		Matcher m = p.matcher(str.trim());
		if (m.matches()) {
			return true;
		}
		return false;
	}

	/**
	 * 判断字符长度
	 *
	 * @param str
	 * @param maxLen
	 * @return
	 */
	public static boolean isLength(String str, int maxLen) {
		char[] cs = str.toCharArray();
		int count = 0;
		int last = cs.length;
		for (int i = 0; i < last; i++) {
			if (cs[i] > 255)
				count += 2;
			else
				count++;
		}
		if (count >= maxLen)
			return true;
		else
			return false;
	}

	/**
	 * 判断是否为空
	 *
	 * @param obj
	 * @return
	 */
	public static boolean isNull(Object obj) {
		if (null == obj || "".equals(obj)) {
			return true;
		}
		return false;
	}

	/**
	 * 判断输入是否是中文
	 *
	 * @param str
	 * @return
	 */
	public static boolean isChinese(String str) {
		String strPattern = "[\u0391-\uFFE5]*";
		Pattern p = Pattern.compile(strPattern);
		Matcher m = p.matcher(str.trim());
		if (m.matches()) {
			return true;
		}
		return false;
	}

	/**
	 * 判断是否为手机号码
	 *
	 * @param phone
	 * @return
	 */
	public static boolean isPhone(String phone) {
		Pattern pattern = Pattern.compile("([1]{1})([0-9]{10})");
		Matcher m = pattern.matcher(phone.trim());
		if (m.matches()) {
			return true;
		}
		return false;
	}

	/**
	 * 判断给定字符串是否空白串。<br>
	 * 空白串是指由空格、制表符、回车符、换行符组成的字符串<br>
	 *
	 * @param input
	 * @return boolean
	 */
	public static boolean isBlank(String data) {
		if (data == null || "".equals(data))
			return true;

		for (int i = 0; i < data.length(); i++) {
			char c = data.charAt(i);
			if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
				return false;
			}
		}
		return true;
	}

}
